<template>
  <div class="header">
    <el-header>
      <div class="user" v-if="userInfo.id">
        <el-avatar :size="36"> <img :src="userInfo.avatar" alt="" /></el-avatar>
        <el-link :underline="false">{{ userInfo.name }}</el-link>
      </div>
      <div class="logout">
        <el-tooltip
          content="logout"
          placement="bottom"
          effect="light"
          :open-delay="1000"
        >
          <template>
            <el-popconfirm
              title="您确认退出黑马面运营后台吗？"
              @confirm="logout"
            >
              <i class="el-icon-switch-button" slot="reference"></i>
            </el-popconfirm>
          </template>
        </el-tooltip>
      </div>
    </el-header>
  </div>
</template>

<script>
import { logoutAPI } from '@/api/user'
import { mapState } from 'vuex'
export default {
  name: 'HeaderView',
  data () {
    return {}
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  methods: {
    // 退出登录
    async logout () {
      await logoutAPI()
      this.$message.success('退出成功')
      this.$store.commit('user/clearToken')
      this.$router.push('/login')
    }
  },
  created () {
    // 获取用户信息
    this.$store.dispatch('user/reqUserInfo')
  }
}
</script>

<style lang="less" scoped>
.el-header {
  display: flex;
  justify-content: flex-end;
  .user {
    display: flex;
    align-items: center;
    background: #fafbfd;
    border: 1px solid #f1f3fa;
    padding: 0 15px;
    span {
      margin-right: 15px;
    }
  }
  .logout {
    width: 50px;
    text-align: center;
    line-height: 62px;
    i {
      font-size: 20px;
      color: #999;
    }
  }
}
</style>
